]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/MapRender/MapVertices.swift
Add license notices
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapVertices.swift
1 /*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
16 */
17 import SwiftUI
18
19 struct MapVertices: View {
20
21 let mapSize: CGSize
22 let vertexSize: CGSize
23 let vertices: [Vertex]
24 let padding = CGFloat(5.0)
25
26 var onDragVertex: (Vertex, CGFloat, CGFloat) -> Void = { _, _, _ in }
27
28 var body: some View {
29 ZStack(alignment: .topLeading) {
30 ForEach(vertices, id: \.id) { vertex in
31 ZStack(alignment: .topLeading) {
32 getVertexShape(vertex).fill(Color.map.vertexColor)
33 Text(vertex.label.replacingOccurrences(of: "\\n", with: "\n")).font(.theme.vertexLabel)
34 .foregroundColor(.map.labelColor)
35 .shadow(color: .white, radius: 0, x: -0.5, y: -0.5)
36 .shadow(color: .white, radius: 0, x: 0.5, y: 0.5)
37 .offset(
38 CGSize(
39 width: w(vertex.position.x) + vertexSize.width + padding,
40 height: h(vertex.position.y) + 7.0))
41 }.gesture(
42 DragGesture()
43 .onChanged { value in
44 let deltaX = value.startLocation.x - value.location.x
45 let deltaY = value.startLocation.y - value.location.y
46 onDragVertex(vertex, deltaX, deltaY)
47 }
48 )
49 }
50 }
51 }
52
53 func h(_ dimension: CGFloat) -> CGFloat {
54 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
55 }
56
57 func w(_ dimension: CGFloat) -> CGFloat {
58 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
59 }
60
61 func getVertexShape(_ vertex: Vertex) -> Path {
62 switch vertex.shape {
63 case .circle:
64 return Path { path in
65 path.addEllipse(
66 in: CGRect(
67 origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
68 ))
69 }
70 case .square:
71 return Path { path in
72 path.addRect(
73 CGRect(
74 x: w(vertex.position.x), y: h(vertex.position.y), width: vertexSize.width,
75 height: vertexSize.height
76 ))
77 }
78 case .triangle:
79 return Path { path in
80 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
81 path.addLine(
82 to: CGPoint(
83 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
84 )
85 path.addLine(
86 to: CGPoint(x: w(vertex.position.x) + vertexSize.width / 2.0, y: h(vertex.position.y)))
87 path.addLine(
88 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
89 path.closeSubpath()
90 }
91 case .x:
92 return Path { path in
93 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)))
94 path.addLine(
95 to: CGPoint(
96 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
97 )
98 path.closeSubpath()
99 path.move(to: CGPoint(x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y)))
100 path.addLine(
101 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
102 path.closeSubpath()
103 }.strokedPath(StrokeStyle(lineWidth: 2.0, lineCap: .butt))
104 }
105 }
106 }
107
108 #Preview {
109 MapVertices(
110 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
111 vertices: [
112 Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)),
113 Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
114 Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
115 Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),
116 ])
117 }